﻿using System;
using System.Collections.Generic;

using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using System.Collections;

namespace INSA
{
    class Util
    {
        private static Excel.Application oExcel = null;

        public static Excel.Application OExcel
        {
            get { return Util.oExcel; }
            set { Util.oExcel = value; }
        }

        private static Excel.Workbook oWorkBook;

        public static Excel.Workbook OWorkBook
        {
            get { return Util.oWorkBook; }
            set { Util.oWorkBook = value; }
        }


        //워크 시트 객체
        private static Excel.Worksheet oSheet;

        public static Excel.Worksheet OSheet
        {
            get { return Util.oSheet; }
            set { Util.oSheet = value; }
        }

        //레인지 객체
        private static Excel.Range oRange;

        public static Excel.Range ORange
        {
            get { return Util.oRange; }
            set { Util.oRange = value; }
        }

        //엑셀파일 이름
        private static string xlsFileName;

        public static string XlsFileName
        {
            get { return Util.xlsFileName; }
            set { Util.xlsFileName = value; }
        }

        public static Excel.Worksheet openXls(string xls_name)
        {

            Util.xlsFileName = xls_name;

            FileInfo finfo = new FileInfo(Util.XlsFileName);

            if (finfo.Exists)
            {
                try
                {
                    OExcel = new Excel.ApplicationClass();
                    OExcel.DisplayAlerts = false;

                    OWorkBook = OExcel.Workbooks.Open(finfo.FullName, 0, false, 5, Config.xls_password, Config.xls_password, false, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
                    
                    OSheet = (Excel.Worksheet)OWorkBook.Sheets[1];

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    OSheet = null;
                }
            }
            else
            {
                MessageBox.Show("프로그램의 구성 요소 중 일부가 삭제되거나 이동되었습니다.   ", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return OSheet;

        }


        //엑셀 닫기
        public static void closeXls()
        {

            try
            {
                if (Util.OWorkBook != null && Util.OSheet != null)
                {
                    
                    OSheet = null;
                    OWorkBook.Close(false, Missing.Value, Type.Missing);
                    OExcel.Quit();
                    if (OExcel != null)
                    {
                        Process[] pProcess;
                        pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");
                        for (int i = 0; i < pProcess.Length; i++)
                        {
                            pProcess[i].Kill();
                        }
                    }
                    OExcel = null;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Util.OSheet = null;
                Util.OWorkBook = null;
                Util.OExcel = null;               

            }
        }


        public static object[,] readXlsMTarget()
        {
            object[,] arr = null;
            try
            {
                Excel.Range range = Util.OSheet.get_Range("A4", "F150");
                arr = (object[,])range.get_Value(Missing.Value);
                return arr;
            }
            catch (Exception ex)
            {             
                throw new Exception(ex.Message);
            }            
        }

        public static void xlsProtect()
        {

            Util.OWorkBook.Protect(Config.xls_password, true, true);
            Util.OSheet.Protect(Config.xls_password, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true);

        }

        public static void xlsUnProtect() 
        {
            Util.OWorkBook.Unprotect(Config.xls_password);
            Util.OSheet.Unprotect(Config.xls_password);
        }

        //랜덤 범위
        public static double getRandomRange(Int16 i)
        {
            if (i <= 10)
            {
                return 0.2;
            }
            else if (i > 10 && i <= 20)
            {
                return 0.1;
            }
            else
            {
                return 0.05;
            }
        }
    }
}
